<!-- 正文开始 -->
<div class="layui-fluid" id="pdaPageSearch">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar" lay-filter="pdaPageSearch">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="searchCode">
                            <option value="">搜索条件</option>
                            <option value="code">编码</option>
                            <option value="title">标题</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input name="searchValue" class="layui-input" type="text" placeholder="请输入"/>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn btnSearch"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button class="layui-btn icon-btn btnAdd"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button class="layui-btn icon-btn btnSave layui-btn-normal"><i class="layui-icon layui-icon-ok"></i>保存</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="pdaPageTable" lay-filter="pdaPageTable"></table>

        </div>
    </div>
</div>

<!-- 表格状态列 -->
<script type="text/html" id="tpl-pdaPageTable-status">
    <input type="checkbox" lay-filter="tpl-pdaPageTable-status" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.status==1 ? 'checked' : ''}}/>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="tpl-pdaPageTable-operator">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑控件</a>
</script>


<script>
    layui.use(['layer', 'table', 'form', 'http','tools'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var http = layui.http;
        var tools = layui.tools;

        var viewId = "#pdaPageSearch";
        var $view = $(viewId);

        form.render('select','pdaPageSearch');

        //渲染表格
        var pdaPageTableIns = table.render({
            elem: '#pdaPageTable',
            url: http.base_server + 'pdaPage/list',
            where: {
            },
            page: false,
            cellMinWidth: 100,
            height: 'full-200',
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: 'ID', hide:true},
                {field: 'code', sort: true, title: '编码',width:100, edit: 'text'},
                {field: 'title', sort: true, title: '标题',width:150, edit: 'text'},
                {field: 'remark', sort: true, title: '备注',width:200, edit: 'text'},
                {field: 'status', sort: true, templet: '#tpl-pdaPageTable-status', title: '状态',width:70, align:"center"},
                {align: 'center', toolbar: '#tpl-pdaPageTable-operator', title: '操作', width: 150},
                {field: 'updateTime', sort: true, title: '更新时间'},
                {field: 'updateByWho', sort: true, title: '更新人'}
            ]],
            error: function(code,msg){
                http.fail(code,msg);
            }
        });

        var fnSearch = function(){
            var param = {};
            var searchCode = $view.find("select[name='searchCode']").val();
            var searchValue = $view.find("input[name='searchValue']").val();
            if(searchCode && searchCode.length>0){
                param[searchCode] = searchValue;
            }
            pdaPageTableIns.reload({where: param});
        }

        // 搜索按钮点击事件
        $view.find(".btnSearch").click(function () {
            fnSearch();
        });

        // 添加按钮点击事件
        $view.find(".btnAdd").click(function () {
            pdaPageTableIns.addRow({status:1});
        });

        // 工具条点击事件
        table.on('tool(pdaPageTable)', function (rowObj) {
            var data = rowObj.data;
            if (rowObj.event === 'edit') { //修改
                if(!data.id){
                    http.failMsg("请先保存");
                    return;
                }
                tools.param = {};
                tools.param.pageId = data.id;
                tools.open({
                    title: 'PDA页面控件--'+data.title,
                    url: http.viewPath + '/system/pdaPageControl.html?v=1',
                    area: ['1124px', '720px'],
                    maxmin: true,
                    resize: true,
                    scrollbar: true,
                    success: function (layero, index) {
                        //$(layero).children('.layui-layer-content').css('overflow', 'visible');  // 禁止出现滚动条
                    },
                    resizing: function(layero){
                        table.resize('pdaPageControlTable', $(layero).height()-350);
                    }
                });
            } else if (rowObj.event === 'del') { //删除
                layer.confirm("确定删除该行?",{btn:["确定","取消"]},
                    function(index,obj) {
                        layer.closeAll('dialog');
                        rowObj.del();
                    }
                );
            }
        });

        // 修改状态
        form.on('switch(tpl-pdaPageTable-status)', function (obj) {
            var idx = obj.othis.parents("tr:eq(0)").attr("data-index");
            var data = {id: obj.elem.value, status: obj.elem.checked ? 1 : 0};
            pdaPageTableIns.saveDelta(idx,data);
        });

        table.on('edit(pdaPageTable)',function (row) {
            var idx = row.tr.attr("data-index");
            var data = {};
            data.id = row.data.id;
            data[row.field] = row.value;
            pdaPageTableIns.saveDelta(idx,data);
        });

        $view.find(".btnSave").click(function () {
            var data = pdaPageTableIns.getDelta();
            if(!data){return;}
            http.post('pdaPage/saveBatch',JSON.stringify(data),function(res){
                if(res.ok){
                    pdaPageTableIns.updateCache(res.data);
                    pdaPageTableIns.resetDelta();
                    http.okMsg(res.msg);
                }
            });
        });

    });

</script>